package com.wt.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import com.wt.dao.OrderInforItemMapper;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.wt.dao.DistributorMapper;
import com.wt.pojo.Distributor;
import com.wt.service.DistributorService;
import com.wt.util.Pager;
import com.wt.util.Pagination;
import com.wt.vo.DistributorIncome;

@Service("DistributorService")
public class DistributorServiceImpl implements DistributorService {

    @Resource
    private DistributorMapper DistributorMapper;
    @Resource
    private OrderInforItemMapper orderInforItemMapper;

    @Override
    public Long selectCountOrders() {
        return orderInforItemMapper.selectCountOrders();
    }

    @Override
    public Distributor getDistributorById(Integer id) {
        return DistributorMapper.selectDistributorById(id);
    }

    @Override
    public Pagination<Distributor> findList(Integer beginDate, Integer endDate, Integer status, Pager pager) {
        Pagination<Distributor> pagination = new Pagination<Distributor>();
        List<Distributor> list = DistributorMapper.findDistributorList(beginDate, endDate, status, pager.getStartIndex(), pager.getPageSize());
        long count = DistributorMapper.findDistributorListCount(beginDate, endDate, status);
        pager.calcPageCount(count);
        pagination.setRecords(list);
        pagination.setPager(pager);

        return pagination;
    }

    @Override
    public Distributor getDistributor(Integer id) {
        Distributor distributor = DistributorMapper.selectByPrimaryKey(id);
        return distributor;
    }

    @Override
    public int updateAuditing(Integer id, Integer status, BigDecimal reviewPoint, Integer reviewStatus) {
        return DistributorMapper.updateAuditing(id, status, reviewPoint, reviewStatus);
    }

    @Override
    public Pagination<Distributor> findReviewList(Integer beginDate, Integer endDate, Integer reviewStatus, Pager pager) {
        // TODO Auto-generated method stub
        Pagination<Distributor> pagination = new Pagination<Distributor>();
        List<Distributor> list = DistributorMapper.findDistributorReviewList(beginDate, endDate, reviewStatus, pager.getStartIndex(), pager.getPageSize());
        long count = DistributorMapper.findDistributorReviewListCount(beginDate, endDate, reviewStatus);
        pager.calcPageCount(count);
        pagination.setRecords(list);
        pagination.setPager(pager);

        return pagination;
    }

    @Override
    public int updateReviewAuditing(Integer id, Integer reviewStatus) {
        Distributor distributor = DistributorMapper.selectByPrimaryKey(id);
        //如果通过复核,则改变积分比例,复核积分比例清空,复核状态改为2:通过审核
        if (reviewStatus.equals(2)) {
            BigDecimal point = distributor.getReviewPoint();
            return DistributorMapper.updateReviewAuditing(id, 2, point);
        } else {
            //如果驳回复核,则只更改复核状态为3:驳回复核
            return DistributorMapper.updateAuditing(id, 2, distributor.getReviewPoint(), reviewStatus);
        }
    }

    @Override
    public Distributor selectDistributor(Integer id) {
        // TODO Auto-generated method stub
        return DistributorMapper.selectByPrimaryKey(id);
    }

    @Override
    public Pagination<DistributorIncome> selectDistributorInCome(Integer id, Long timeStart, Long timeEnd,
                                                                 Pager pager) {
        List<DistributorIncome> distributorIncomes = DistributorMapper.selectDistributorInCome(
                id, timeStart, timeEnd, pager.getStartIndex(), pager.getPageSize());




        Pagination<DistributorIncome> pagination = new Pagination<DistributorIncome>();
        if (id != null) {
            for (DistributorIncome distributorIncome : distributorIncomes) {
                BigDecimal totalWorth = distributorIncome.getOrderWorth();
                distributorIncome.setCommission(totalWorth.multiply(DistributorMapper.selectByPrimaryKey(id).getProportion()));

            }
        }
        Long count = (long) DistributorMapper.selectDistributorInComeCount(id, timeStart, timeEnd).size();
        pager.calcPageCount(count);
        pager.setRowCount(count);
        pagination.setRecords(distributorIncomes);
        pagination.setPager(pager);
        return pagination;
    }

    @Override
    public int updateCommissionWithdraw(Integer id, BigDecimal commissionWithdraw) {
        // TODO Auto-generated method stub
        return DistributorMapper.updateCommissionWithdraw(id, commissionWithdraw);
    }

}
